package goulu.bloggy.com.Activities;

import goulu.bloggy.com.R;
import goulu.bloggy.com.Connections.AccountMysqlConnection;
import goulu.bloggy.com.Helpers.LoginHelper;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class NewAccountActivity extends Activity implements OnClickListener 
{
	EditText username;
	EditText password;
	EditText repassword;
	TextView errorText;
	
	Handler mysqlResultHandler = new Handler() 
	{
		@Override
		public void handleMessage(Message msg) 
		{
			if(msg.what == AccountMysqlConnection.ADD_ACCOUNT)
			{
				Toast.makeText(getApplicationContext(), (String)msg.obj, Toast.LENGTH_LONG).show();
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createaccview);
		this.setupView();
	}

	private void setupView() 
	{
		username = (EditText)findViewById(R.id.createaccInputusername);
		password = (EditText)findViewById(R.id.createaccInputpassword);
		repassword = (EditText)findViewById(R.id.createaccInputrepassword);
		errorText = (TextView)findViewById(R.id.createaccErrorText);
		Button submitBT = (Button)findViewById(R.id.createaccSubmitBT);
		submitBT.setOnClickListener(this);
		Button backBt = (Button)findViewById(R.id.createaccBackBT);
		backBt.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) 
	{
		switch (v.getId()) {
		case R.id.createaccSubmitBT:
			if(this.inputIsValid())
			{
				Log.d("NewAccountActivity.onClick()", "Input valid, preparing mysql insert");
				String uname = username.getText().toString();
				String encryptedpw = LoginHelper.Encrypt(password.getText().toString());
				password.setText("");
				repassword.setText("");
				AccountMysqlConnection conn = new AccountMysqlConnection(mysqlResultHandler, AccountMysqlConnection.ADD_ACCOUNT, uname, encryptedpw);
				Thread t = new Thread(conn);
				t.start();
				finish();
			}
			break;
		case R.id.createaccBackBT:
			finish();
			break;

		}
		
	}

	private boolean inputIsValid() 
	{
		boolean isValid = true;
		int userlength = username.getText().toString().length();
		int passwordlength = password.getText().toString().length();
		errorText.setText("");
		if(userlength < 3 || userlength > 10)
		{
			isValid = false;
			errorText.append("Username length invalid! \n");
		}
		
		if(passwordlength < 3 || passwordlength > 15)
		{
			isValid = false;
			errorText.append("Password length invalid! \n");
		}
		
		if(!repassword.getText().toString().equals(password.getText().toString()))
		{
			isValid = false;
			errorText.append("Password is different \n from the re-typed one! \n");
			password.setText("");
			repassword.setText("");
		}
		
		return isValid;
	}
	
}
